package com.cross.core;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import com.cross.util.StringUtils;

/**
 * 一个数据元,如:在excel中为一个单元格
 * 
 * @date 2009-2-4
 * @author craziness_stone
 */
public final class Item<T> implements Cloneable {

	// 数据
	private T object;
	// 行号
	private int column;
	// 数据类型
	private String type;
	//精度
	private int precision;

	// 构造方法
	public Item() {
	};

	// 构造方法
	public Item(T object, String type, int precision) {
		this.object = object;
		this.type = type;
		this.precision = precision;
	}
	
	// 构造方法
	public Item(T object, String type) {
		this.object = object;
		this.type = type;
	}

	/**
	 * 获得数据元的内容
	 * 
	 * 2009-2-4
	 * 
	 * @author craziness_stone
	 * @return
	 */
	public T get() {
		return object;
	}

	/**
	 * 设置数据
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone cc
	 * @param value
	 */
	public void set(T value) {
		this.object = value;
	}

	/**
	 * 获得字符串
	 * 
	 * Mar 8, 2009
	 * 
	 * @author craziness_stone cc
	 * @return
	 */
	public String getString() {
		if (null == object) {
			return null;
		}
		return object.toString();
	}

	/**
	 * 获得整型数据
	 * 
	 * Mar 8, 2009
	 * 
	 * @author craziness_stone cc
	 * @return
	 */
	public Integer getInteger() {
		if (null == object || StringUtils.isBlank(object.toString())) {
			return null;
		}
		return new Integer(object.toString());
	}

	/**
	 * 获得布尔型数据
	 * 
	 * Mar 8, 2009
	 * 
	 * @author craziness_stone cc
	 * @return
	 */
	public Boolean getBoolean() {
		if (null == object || StringUtils.isBlank(object.toString())) {
			return null;
		}
		if("true".equalsIgnoreCase(object.toString()) || "1".equalsIgnoreCase(object.toString()) || "Y".equalsIgnoreCase(object.toString()) || "YES".equalsIgnoreCase(object.toString())){
			return Boolean.TRUE;
		} else if("false".equalsIgnoreCase(object.toString()) || "0".equalsIgnoreCase(object.toString()) || "N".equalsIgnoreCase(object.toString()) || "NO".equalsIgnoreCase(object.toString())){
			return Boolean.FALSE;
		}
		if(object instanceof Boolean){
			return (Boolean)object;
		}
		return new Boolean(object.toString());
	}

	/**
	 * 获得长整型数据
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone
	 * @return
	 */
	public Long getLong() {
		if(null == object || StringUtils.isBlank(object.toString())){
			return null;
		}
		return Long.parseLong(object.toString());
	}

	/**
	 * 获得双精度数据
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone cc
	 * @return
	 */
	public Double getDouble() {
		if(null == object || StringUtils.isBlank(object.toString())){
			return null;
		}
		return Double.parseDouble(object.toString());
	}

	/**
	 * 获得日期型数据
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone
	 * @return
	 */
	public Date getDate() {
		if(null == object || StringUtils.isBlank(object.toString())){
			return null;
		}
		try {
			return DateFormat.getDateInstance().parse(object.toString());
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 获得列号
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone cc
	 * @return
	 */
	public Number getNumber() {
		if(null == object || StringUtils.isBlank(object.toString())){
			return null;
		}
		return (Number)Float.parseFloat(object.toString());
	}

	public Time getTime(){
		if(null == object || StringUtils.isBlank(object.toString())){
			return null;
		}
		return Time.valueOf(object.toString());
	}
	
	public Timestamp getTimestamp(){
		if(null == object || StringUtils.isBlank(object.toString())){
			return null;
		}
		if(object instanceof Timestamp){
			return (Timestamp)object;
		} else if(object instanceof String){
			try{
				return Timestamp.valueOf(object.toString());
			}catch(RuntimeException e){
				return null;
			}
		} else {
			return null;
		}
	}
	/**
	 * 获得字符数据
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone cc
	 * @return
	 */
	public Character getCharacter() {
		if(null == object){
			return null;
		}
		return Character.valueOf((object.toString().charAt(0)));
	}

	/**
	 * 获得字节数据
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone
	 * @return
	 */
	public Byte getByte() {
		if(null == object){
			return null;
		}
		return (Byte)object;
	}

	/**
	 * 获得单精度数据
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone cc
	 * @return
	 */
	public Float getFloat() {
		if(null == object){
			return null;
		}
		return Float.parseFloat(object.toString());
	}

	/**
	 * 获得短整型数据
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone
	 * @return
	 */
	public Short getShort() {
		if(null == object){
			return null;
		}
		return (Short)object;
	}

	/**
	 * 设置数据类型
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone cc
	 * @param type
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * 是否为空
	 * 
	 * 2009-2-5
	 * 
	 * @author craziness_stone
	 * @return true:空;false:不空
	 */
	public boolean empty() {
		if (null == object) {
			return true;
		}
		return StringUtils.isEmpty(object.toString());
	}

	/**
	 * 获得数据类型
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone cc
	 * @return
	 */
	public String getType() {
		return type;
	}


	/**
	 * 克隆方法
	 * 
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone
	 * @return
	 */
	public Item clone() {
		Item item = new Item();
		item.setType(this.getType());
		item.set(this.get());
		return item;
	}

	/**
	 * 获得列号
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone
	 * @return
	 */
	public int getColumn() {
		return column;
	}

	/**
	 * 设置列号
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone 
	 * @param column
	 */
	public void setColumn(int column) {
		this.column = column;
	}

	public int getPrecision() {
		return precision;
	}

	public void setPrecision(int precision) {
		this.precision = precision;
	}
}
